<?php 
import('miho.router.*');

/**
* 
*/
class PatternRoute implements IRoute
{
  /**
   * Name of a route
   *
   * @var string
   */
  protected $_name;
  
  /**
   * Route pattern
   *
   * @var string
   */
  protected $_value;
  
  public function __construct($name){
    $this->_name = $name;
    $this->_parameters = array();
  }
  
  public function getName(){
      return $this->_name;
  }
  
  public function setName($value){
    $this->_name = $value;
  }
  
  public function getValue(){
    return $this->_value;
  }
  
  public function setValue($value){
    $this->_value = $value;
  }
  
  public function __get($name){
    return isset($this->_parameters[$name]) ? $this->_parameters[$name] : null;
  }
  
  public function getParameters(){
    return $this->_parameters;
  }
  
  public function setParameter($key,$value){
    $this->_parameters[$key] = $value;
    //var_dump($key);
  }
  
  public function isMatches($url){
    $urlItems   = explode('/', $url);
    $patterns   = explode('/', $this->_value);
    $parameters = array();
    
    foreach ($patterns as $pos=>$key) {
      //url is longer than route, false
      if(!array_key_exists($pos, $urlItems)){
        return false;
      }
      
      if(substr($key, 0, 1) == ':'){
        $key = substr($key, 1);
        $parameters[$key] = $urlItems[$pos];
      }else{
        if($urlItems[$pos] != $key) return false;
      }
      
    }
    
    foreach ($parameters as $key => $value) {
      $this->_parameters[$key] = $value;  
    }
    //echo('params');
    //var_dump($this->_parameters);
    //$this->_parameters = $parameters;
    
    return true;
  }
  
}


?>